<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Subject;
use DB;

class SubjectController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $subject_name = json_encode(
            DB::table('subject')->Select('subject_name')->get()->map(function ($value) {
                return (array)$value;
            })->toArray()//laravel 直接将搜索变成数组,搜索在表格显示搜索框模糊提示的数据;
        );
        $data['subject_name']  = $subject_name;
        return view('admin.subject.subject-list_1',$data)->with('subject_name',$subject_name);
        //datatable 是读取视图后再由视图js读取ajax_list方法读取表格显示数据
    }

    public function index1()
    {
        //
//        $subject_name = json_encode(
//            DB::table('subject')->Select('subject_name')->get()->map(function ($value) {
//                return (array)$value;
//            })->toArray()//laravel 直接将搜索变成数组,搜索在表格显示搜索框模糊提示的数据;
//        );
//        $data['subject_name']  = $subject_name;
//        return view('admin.subject.subject-list',$data)->with('subject_name',$subject_name);
        return view('admin.subject.subject-list');
        //datatable 是读取视图后再由视图js读取ajax_list方法读取表格显示数据
    }

    /**
     * DataTable ajax subject list data.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  App\Models\Subject;
     * @return \Illuminate\Http\Response
     */
    public function ajax_list(Request $request,Subject $subject)
    {
        if(!$request->ajax()){
            return ["STATUS"=>"ERROR","CODE"=>1];
        }
//        dd($request);
        //搜索条件
        $kerword = $request->get('name_search');//关键字查询
        $logmin = $request->get('logmin');//开始时间
        $logmax = $request->get('logmax');//结束时间
        $logmin = isset($logmin)?$logmin:'';
        $logmax = isset($logmax)?$logmax.' 23:59:59':'';
        $offset = $request->get('iDisplayStart');//每次从多少条数据开始查询，偏移量
        $row = $request->get('iDisplayLength');//每次显示的条数
        //时间区间
        $params = [];
        if (!empty($logmin) && !empty($logmax)) {
            if ($logmax >= $logmin) {
                $params['Range']['created_at'] = [$logmin, $logmax];
            }
        } elseif (empty($logmax) && !empty($logmin)) {
            $params['Range']['created_at'] = [$logmin,''];
        } elseif (!empty($logmax) && empty($logmin)) {
            $params['Range']['created_at'] = [0, $logmax];
        }else{
        }

//        $params['Range']['created_at'] = [$logmin, $logmax];
        isset($kerword)?$params['Key']['subject_name']=[$kerword]:'';
        isset($row)?$params['Limit']['limit']=[$row]:'';
        isset($offset)?$params['Offset']['Offset']=[$offset]:'';

        //根据搜索条件搜索结果总条数
//        $obj  = DB::table('subject')->Select('subject_name','logo','deleted_at','created_at','updated_at');
        $obj  = DB::table('subject')->Select('*');
        if (!empty($logmin)) {
            $obj1 = $obj->where('created_at','>=',$logmin);
        }else{
            $obj1 = $obj;
        }
        if (!empty($logmax)) {
            $obj1 = $obj1->where('created_at','<=',$logmax);
        }
        if (!empty($kerword)) {
            $obj1 = $obj1->where('subject_name','like','%'.$kerword.'%');
        }
        $cnt_data = $obj1->count();

        //根据条件进行具体数据字段搜索
        $data = $this->setWhere($obj,$params)->get()->map(function ($value) {
            return (array)$value;
        })->toArray();//laravel 直接将搜索变成数组,搜索在表格显示的数据

        //将搜索结果按照某个字段进行排序
        if(!empty($request->get('iSortCol_0'))){
            $iSortCol_0 = $request->get('iSortCol_0');//获取排序的列数
            $sSortDir_0 = $request->get('sSortDir_0');//获取排序的规则 asc /desc
            $filter_order = $request->get('mDataProp_'.$iSortCol_0);//获取排序的字段，从搜索结果中排序，
            //将name提取出来存储到一维数组里
            $fiter_by = array();
            foreach ($data as $v) {
                $fiter_by[] = $v["$filter_order"];
            }
            //按照这个字段进行升序或降序排序
            $sSortDir_0 == 'asc'? array_multisort($fiter_by, SORT_ASC, $data):array_multisort($fiter_by, SORT_DESC, $data);
        }

//        dd($data);
//$data = json_encode($data);
        $info = [
            'draw'=>$request->get('draw'),//返回datatable表必须字段
            'recordsTotal'=>$cnt_data,//总记录数
            'recordsFiltered'=>$cnt_data,//查询结果数？
            'data'=>$data,//返回数据
        ];
//        dd($info);
        return json_encode($info);
//        return $info;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function subject_pic_upload()
    {
        //
        echo 'create';die;
    }



    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        echo 'create';die;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        echo 'store';die;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        //
//        echo 'show';die;

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        echo 'edit';die;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        echo 'update';die;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        echo 'destroy';die;
    }

    /**
     * DataTable ajax subject list data set where.
     *
     * @param  object $obj
     * @param  marry $params = []
     * @return object $obj
     */
    private function setWhere($obj,$params = [])
    {
        $where  = '';
        foreach($params as $key=>$val) {
            switch($key) {
                //过滤条件
//                case 'Filter'://字段值
//                    foreach($val as $Filterkey=>$Filterval) {
//                        if (count($Filterval)) {
//                            $obj = $obj->where($Filterkey,'=',$Filterval[0]);
//                        }
//                    }
//                    break;
//                case 'NotFilter':
//                    foreach($val as $Filterkey=>$Filterval)
//                    {
//                        $this->client->SetFilter($Filterkey,$Filterval,true);
//                    }
//                    break;
//                case 'FloatRange' :
//                    foreach($val as $Filterkey=>$Filterval)
//                    {
//                        $this->client->SetFilterFloatRange($Filterkey,$Filterval[0],$Filterval[1]);
//                    }
//                    break;
                case 'Range' ://字段值范围
                    foreach($val as $Filterkey=>$Filterval)
                    {
                        if (!empty($Filterval[0])) {
                            $obj = $obj->where($Filterkey,'>=',$Filterval[0]);
                        }
                        if (!empty($Filterval[1])) {
                            $obj = $obj->where($Filterkey,'<=',$Filterval[1]);
                        }
                    }
                    break;
                case 'Key'://关键词查询值
                    foreach($val as $Filterkey=>$Filterval) {
                        if (count($Filterval)) {
                            $obj = $obj->where($Filterkey,'like','%'.$Filterval[0].'%');
                        }
                    }
                    //$val = $this->FilterString($val);
                    /* $this->client->AddQuery( $val, Yii::$app->params['sphinxIndex'] );*/
                    break;
                // 字段按照排序

                case 'Limit'://每次查询条数
                    foreach($val as $Filterkey=>$Filterval) {
                        if (count($Filterval)) {
                            $obj = $obj->limit($Filterval[0]);
                        }
                    }
                    //$val = $this->FilterString($val);
                    /* $this->client->AddQuery( $val, Yii::$app->params['sphinxIndex'] );*/
                    break;
                case 'Offset'://偏移量，分页
                    foreach($val as $Filterkey=>$Filterval) {
                        if (count($Filterval)) {
                            $obj = $obj->offset($Filterval[0]);
                        }
                    }
                    break;
//                case 'Orderby'://按照某个字段排序升序或降序
//                    foreach($val as $Filterkey=>$Filterval) {
//                        if (!empty($Filterval)) {
//                            $obj = $obj->orderby($Filterval[0],$Filterval[1]);
//                        }
//                    }
//                    break;
            }
        }

        return $obj;
    }


    /**
     * subject add data.
     */
    public function subject_add()
    {
//        echo 'hello';die;
        return view('admin.subject.subject_add');
    }

    /**
     * subject search data.
     * @param  \Illuminate\Http\Request  $request
     * @param  App\Models\Subject;
     */
//    public function subject_search(Request $request)
//    {
//        if(!$request->ajax()){
//            return ["STATUS"=>"ERROR","CODE"=>1];
//        }
////        $kerword = $request->get('sSearch');
//        $start_time = isset($request->get['logmin'])? $request->get['logmin'] : '' ;
//        $end_time = isset($request->get['logmax'])? $request->get['logmax'] : '' ;
//        $data  = DB::table('subject')->Select('*')->where('created_at','>',$start_time)->where('created_at','<',$end_time)->get();
//
//        $cnt = count($data);
//        $info = [
//            'draw'=>$request->get('draw'),
//            'recordsTotal'=>$cnt,
//            'recordsFiltered'=>$cnt,
//            'data'=>$data,
//        ];
//        return $info;
//    }
}
